package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.VocChapterConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocChapBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocChapterDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapterMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteChapterOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapterDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocChapterCommand;
import com.cskaoyan.wordmemorize.request.VocChapterPageRequest;
import com.cskaoyan.wordmemorize.service.VocChapterService;
import org.mapstruct.ap.shaded.freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VocChapterServiceImpl extends ServiceImpl<VocChapterMapper, VocChapterDO> implements VocChapterService {

    @Autowired
    private VocChapterConverter vocChapterConverter;

    @Autowired
    private VocChapterMapper vocChapterMapper;

    @Autowired
    private VocChapBookMapper vocChapBookMapper;
    @Override
    public PageDTO<VocChapterDTO> getChapterPage(VocChapterPageRequest pageQuery) {

        if (pageQuery.getPageNum() == null){
            //刚开始时，查询所有章节
            List<VocChapterDO> vocChapterDOS = vocChapterMapper.selectList(null);
            return vocChapterConverter.vocChapterDOs2Page(vocChapterDOS, (long) vocChapterDOS.size());
        }
        // 分页查询
        Page<VocChapterDO> vocChapterDOPage = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());
        LambdaQueryWrapper<VocChapterDO> vocChapterDOQueryWrapper = new LambdaQueryWrapper<>();
        //模糊匹配
        if (StringUtil.emptyToNull(pageQuery.getKeyword()) != null){
            vocChapterDOQueryWrapper.like(VocChapterDO::getChapterName, pageQuery.getKeyword());
        }
        //获取该书籍下的章节
        vocChapterDOQueryWrapper.eq(VocChapterDO::getBookId, pageQuery.getBookId());
        Page<VocChapterDO> vocChapterPage = vocChapterMapper.selectPage(vocChapterDOPage, vocChapterDOQueryWrapper);

        return vocChapterConverter.vocChapterDOs2Page(vocChapterPage.getRecords(), vocChapterPage.getTotal());
    }

    @OperateLog(dateType = "章节", operateType = OperateLogConstant.OPERATE_ADD)
    @Override
    public void addChapter(VocChapterCommand command) {
        System.out.println("以下是新增章节");
        VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
        int insert = vocChapterMapper.insert(vocChapterDO);
        if (insert == 0){
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
    }

    @OperateLog(dateType = "章节", operateType = OperateLogConstant.OPERATE_UPDATE)
    @Override
    public void updateChapter(VocChapterCommand command) {
        System.out.println("以下是更新章节");
        VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
        System.out.println("转化前" + vocChapterDO);
        vocChapterDO.setId(Long.valueOf(command.getId()));

        int update = vocChapterMapper.updateById(vocChapterDO);
        if (update == 0){
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
    }

    @OperateLog(dateType = "章节", operateType = OperateLogConstant.OPERATE_SINGLE_DELETE)
    @Transactional
    @Override
    public DeleteChapterOperateDTO deleteChapter(Long chapterId) {
        System.out.println("以下是删除指定的章节");
        VocChapterDO vocChapterDO = vocChapterMapper.selectById(chapterId);
        int row1 = vocChapterMapper.deleteById(chapterId);

        Map<String, Object> map = new HashMap<>();
        map.put("chapter_id", chapterId);
        int row2 = vocChapBookMapper.deleteByMap(map);

        if (row1 <= 0 || row2 < 0){
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        return vocChapterConverter.vocChapterDO2DeleteDTO(vocChapterDO);
    }

    @OperateLog(dateType = "章节", operateType = OperateLogConstant.OPERATE_BATCH_DELETE)
    @Transactional
    @Override
    public List<DeleteChapterOperateDTO> deleteChapters(BatchDeleteCommand command) {
        System.out.println("以下是批量删除章节");
        List<VocChapterDO> vocChapterDOS = vocChapterMapper.selectByIds(command.getIds());
        int row1 = vocChapterMapper.deleteByIds(command.getIds());

        //连表删除，删除voc_chap_book表中的相关书籍
        LambdaQueryWrapper<VocChapBookDO> vocChapBookDOQueryWrapper = new LambdaQueryWrapper<>();
        vocChapBookDOQueryWrapper.in(VocChapBookDO::getChapterId, command.getIds());
        int row2 = vocChapBookMapper.delete(vocChapBookDOQueryWrapper);

        if (row1 <= 0 || row2 < 0){
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

        return vocChapterConverter.vocChapterDOs2DeleteDTOs(vocChapterDOS);
    }
}
